package com.zyq.exportDir;

import com.jfinal.kit.PropKit;
import com.jfinal.plugin.IPlugin;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.druid.DruidPlugin;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.ext.gyProject.utils.ConfigUtils;

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by 邹宇泉 on 2017/11/20.
 */
public class Run {
    private static Map<String, IPlugin[]> plugins = new HashMap<>();

    public static void createDatabasePool(String pre) {
        try {
            DruidPlugin druidPlugin = ConfigUtils.readFactory(pre);
            ActiveRecordPlugin activeRecordPlugin = new ActiveRecordPlugin(pre, druidPlugin);
            druidPlugin.start();
            activeRecordPlugin.start();
            plugins.put(pre, new IPlugin[]{druidPlugin, activeRecordPlugin});
        } catch (Exception e) {
            System.out.println(pre + "数据库初始化失败");
        }
    }

    public static void stopDatabasePool(String pre) {
        IPlugin[] iPlugins = plugins.get(pre);
        for (IPlugin iPlugin : iPlugins) {
            iPlugin.stop();
        }
    }

    public static void createIds(File config, String path) {
        PropKit.use(config);
        String dataBaseNames[] = PropKit.get("actives").split(",");

        Stream<String> stream = Arrays.stream(dataBaseNames);
        stream.forEach(Run::createDatabasePool);
        File save_dir = new File(path);
        if (save_dir.exists()) {
            try {
                FileUtils.deleteDirectory(save_dir);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        save_dir.mkdirs();

        Arrays.stream(dataBaseNames).parallel().forEach(s -> {
            Set<String> set = new HashSet<>();
            String sql = PropKit.get(s + "_sql");
            List<Record> records = new ArrayList<>();
            Arrays.stream(sql.split("###")).parallel().forEach(sss -> {
                List<Record> rr = Db.use(s).find(sss);
                records.addAll(rr);
            });
            records.forEach(r -> {
                String attachmentId = r.getStr("ATTACHMENT_ID");
                if (attachmentId != null) {
                    String[] split = attachmentId.split(",");
                    Arrays.stream(split).forEach(id -> {
                        if (!StringUtils.isBlank(id)) {
                            set.add(id);
                        }
                    });
                }
            });
            try {
                FileUtils.writeStringToFile(new File(save_dir, s), StringUtils.join(set.stream().sorted((s1, s2) -> new Long(s1).compareTo(Long.parseLong(s2))).collect(Collectors.toList()), ","), "utf-8");
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        Arrays.stream(dataBaseNames).parallel().forEach(Run::stopDatabasePool);
    }
}